const db = wx.cloud.database()
const app = getApp()

// 处理任务书-道具节点
function do_control_question_tool(id) {
  console.log('do_control_question_tool', id)
  let tool_list = app.globalData.tool_list
  let question = app.globalData.config.question_list[id]

  // 如果已经有该道具，计数加1
  for (let key in tool_list) {
    if (tool_list[key].id == question.answer) {
      tool_list[key].count++
        return
    }
  }

  // 新建道具
  let tool = {
    id: question.answer,
    type: question.items,
    count: 1
  }
  tool_list.push(tool)
}

// 处理任务书-关卡节点
function do_control_question_checkpoint(id, status) {
  console.log('do_control_question_checkpoint', id)
  let record_list = app.globalData.record_list

  // 如果已经有记录，修改
  console.log(record_list)
  for (let key in record_list) {
    let record = record_list[key]
    console.log('record', record)
    if (record_list[key].id == id) {
      if ('show' in status) {
        record_list[key].show = status.show
      }
      if ('status' in status) {
        record_list[key].status = status.status
      }
      return
    }
  }

  // 没有匹配到记录，创建一条新记录
  let record = {
    id: id,
  }
  if ('show' in status) {
    record.show = status.show
  }
  if ('status' in status) {
    record.status = status.status
  }
  record_list.push(record)

}

// 处理任务书数据节点
function do_control_question(items) {
  console.log('do_control_question', items)
  // let game_code = app.globalData.game_code
  // let record_id = app.globalData.record_id

  // 解析晋级书内容
  let item_list = items.split(';')
  for (let key in item_list) {
    // console.log(key, item_list[key], item_list[key].substr(0, 1), item_list[key].substr(1))
    let method = item_list[key].substr(0, 1)
    let id = item_list[key].substr(1)
    let status = null
    switch (method) {
      case '+':
        // 显示关卡
        status = {
          show: true,
        }
        do_control_question_checkpoint(id, status)
        break

      case '-':
        // 隐藏关卡
        status = {
          show: false,
        }
        do_control_question_checkpoint(id, status)
        break

      case 's':
        // 设置关卡状态（关卡ID在当前任务task中）
        status = {
          status: id,
        }
        do_control_question_checkpoint(app.globalData.task.cid, status)
        break

      case '*':
        // 添加道具（道具卡/宝藏卡）
        do_control_question_tool(id)
        break
    }
  }
}

// 获取下一个题目ID
function get_next_question_id(question_id) {
  console.log('get_next_question')
  let question = app.globalData.config.question_list[question_id]
  let task = app.globalData.task

  // 下一道题目ID
  let next_question_id = 0
  if (question.type == 12) {
    // 如果是晋级书，直接获取下一个题目ID
    next_question_id = question.true_id
  } else {
    next_question_id = task.success ? question.true_id : question.false_id
  }

  if (next_question_id == 0) {
    task.question = null
    return 0
  }

  return next_question_id
}

// 从题库中随机抽取一题
function get_random_question(items) {
  let question_list = items.split(',')
  let length = state.task.question_list.length

  if (length > 0) {
    // 随机一个index
    return Math.floor(Math.random() * length)
  }

  // 错误
  return -1
}

// 设置题目
exports.set_question = (question_id) => {
  console.log('set_question', question_id)
  let game_code = app.globalData.game_code
  let record_id = app.globalData.record_id
  let task = app.globalData.task // 需要判断是否存在任务
  let question = null
  let temp_question_id = question_id

  while (true) {
    let temp_question = app.globalData.config.question_list[temp_question_id]
    console.log('temp_question', temp_question)
    if (temp_question.type == 0) {
      // 如果是题库，随机抽取一题
      qid = get_random_question(temp_question.items)
      question = app.globalData.config.question_list[qid]
    } else {
      // 非题库，直接选择题目
      question = temp_question
    }

    // 对晋级书单独处理
    if (question.type == 12) {
      // do....
      do_control_question(question.items)

      // 获取下一个题目ID
      temp_question_id = get_next_question_id(question.id)
    } else {
      // 非任务数据，跳出循环
      break
    }
  }

  // 在本地任务记录中记录题目信息
  task.qid = question.id
  task.answer = ''
  task.success = false
  task.mark = 0

  // 更新远程记录
  console.log('update task', app.globalData)
  db.collection(game_code).doc(record_id).update({
      data: {
        update_time: new Date(),
        record_list: app.globalData.record_list,
        tool_list: app.globalData.tool_list,
        task: task,
      }
    }).then(res => {})
    .catch(console.error)
}

// 回答问题
exports.answer_question = (answer) => {
  let game_code = app.globalData.game_code
  let record_id = app.globalData.record_id
  let task = app.globalData.task
  let answer_list = app.globalData.answer_list
  let question = app.globalData.config.question_list[task.qid]

  // 在本地任务记录中记录题目信息
  task.answer = answer
  task.success = true
  task.mark = question.mark == '' ? 0 : parseInt(question.mark)

  let item = {
    cid: task.cid,
    qid: task.qid,
    answer: answer,
    success: true,
    mark: task.mark,
    time: new Date(),
  }
  answer_list.push(item)

  // 更新远程记录
  console.log('update answer', app.globalData)
  db.collection(game_code).doc(record_id).update({
      data: {
        update_time: new Date(),
        answer_list: answer_list,
      }
    }).then(res => {})
    .catch(console.error)

  // 获取下一题
  let next_question_id = get_next_question_id(question.id)
  if (next_question_id == 0) {
    // 没有题目了，清理当前任务
    app.globalData.task = {}
    console.log('task', app.globalData.task)
    return next_question_id
  }
  exports.set_question(next_question_id)
  return next_question_id
}